﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using System.Collections;
using Castle.ActiveRecord.Queries;

namespace MxTol.Models
{
    /// <summary>
    /// 管理员实体类
    /// </summary>
    [ActiveRecord(Table = "Admin")]
    public class Admin : ActiveRecordBase
    {
        /// <summary>
        /// 管理员信息主键
        /// </summary>
        [PrimaryKey("id")]
        public Guid ID { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [Property("name")]
        public string Name { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        [Property("phone")]
        public string Phone { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        [Property("account")]
        public string Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Property("psword")]
        public string Psword { get; set; }

        /// <summary>
        /// 该条记录添加时间
        /// </summary>
        [Property("add_time")]
        public DateTime AddTime { get; set; }

        /// <summary>
        /// 管理员级别   0：管理员   1：超级管理员  2：系统管理员
        /// </summary>
        [Property("level")]
        public int Level { get; set; }

        /// <summary>
        /// 获取所有管理员
        /// </summary>
        public static List<Admin> GetAllAdmin()
        {
            List<Admin> list = null;

            IList admins = FindAll(typeof(Admin));
            if (admins != null && admins.Count > 0)
            {
                list = new List<Admin>();
                foreach (var admin in admins)
                {
                    list.Add(((Admin)admin));
                }
                return list;
            }

            return list;
        }

        /// <summary>
        /// 根据顶级管理员，即系统管理员
        /// </summary>
        public static Admin GetTopLevelAdmin()
        {
            IList list = FindAllByProperty(typeof(Admin), "Level", 2);
            if (list != null && list.Count > 0)
            {
                Admin admin = list[0] as Admin;
                return admin;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据管理员账号获取管理员
        /// </summary>
        public static Admin GetAdminByAccount(string account)
        {
            IList list = FindAllByProperty(typeof(Admin), "Account", account);
            if (list != null && list.Count > 0)
            {
                Admin admin = list[0] as Admin;
                return admin;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据主键获取管理员
        /// </summary>
        public static Admin GetAdminByID(Guid id)
        {
            Admin admin = (Admin)FindByPrimaryKey(typeof(Admin), id);
            if (admin != null)
            {
                return admin;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除所有管理员
        /// </summary>
        public static void DeleteAll()
        {
            DeleteAll(typeof(Admin));
        }
    }
}
